﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;


namespace Sandbox
{
	class GrouperVoot
	{
		static void Main(string[] args)
		{
			
			//CallRestMethod(url);
			CallGrouper();
		}

		public static void CallGrouper()
		{
			string url = "https://authz.geant.org/grouper-ws/voot/groups";
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "GET";
			//request.ContentType = "application/json";

			string autorization = "GrouperSystem" + ":" + "dante";
			byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
			autorization = Convert.ToBase64String(binaryAuthorization);
			autorization = "Basic " + autorization;
			request.Headers.Add("AUTHORIZATION", autorization);

			string json = string.Empty;


				try
				{

					// call grouper voot web service
					HttpWebResponse response = (HttpWebResponse)request.GetResponse();
					if (response.StatusCode != HttpStatusCode.OK)
					{
						//LogHelper.LogError(("http status code : " + response.StatusCode), response.Headers.ToString());
					}
					else
					{
						// store the response in a string
						using (Stream responseStream = response.GetResponseStream())
						{
							StreamReader stream = new StreamReader(responseStream, Encoding.UTF8);
							json = stream.ReadToEnd();
							stream.Close();
						}

						// transform the json string to .Net object
						var res = JsonConvert.DeserializeObject<GrouperEntry>(json);

						// transform grouper entry to dataElement
						foreach (Entry item in res.entry)
						{
							Console.WriteLine( item.id);

						}
					}
				}
				catch (WebException ex)
				{
					WebResponse errorResponse = ex.Response;
					using (Stream responseStream = errorResponse.GetResponseStream())
					{
						StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
						String errorText = reader.ReadToEnd();
						//LogHelper.LogError(ex, errorText);

					}
				}
		}

		public static void LoadParam()
		{
			//string json = System.IO.File.ReadAllText(@"C:\dev\cnrsccp\Sandbox\allGroups.txt");
			string json = System.IO.File.ReadAllText(@"C:\CNRSccp\Farm.json");

			var res = JsonConvert.DeserializeObject<Params>(json);


			JsonTextReader reader = new JsonTextReader(new StringReader(json));
			while (reader.Read())
			{
				if (reader.Value != null)
					Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
				else
					Console.WriteLine("Token: {0}", reader.TokenType);
			}

		}

		public static void LoadVoot()
		{
			string json = System.IO.File.ReadAllText(@"C:\dev\cnrsccp\Sandbox\allGroups.txt");

			var res = JsonConvert.DeserializeObject<GrouperEntry>(json);


			JsonTextReader reader = new JsonTextReader(new StringReader(json));
			while (reader.Read())
			{
				if (reader.Value != null)
					Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
				else
					Console.WriteLine("Token: {0}", reader.TokenType);
			}

		}

	

	}
}